//
//@File    :	12.3_vegnews.cpp
//@Time    :	2021/09/01 12:31:16
//@Auther  :	Sun Sheng
//@Contact :	970340843@qq.com
//@Desc    :	using new anf delete with classes
//

//complie with strngbad.cpp

#include <iostream>
using std::cout;
#include "12.1_strngbad.h"

void callme1(StringBad &); //pass by reference
void callme2(StringBad);   //pass by value

int main()
{
    using std::endl;
    {
        cout << "Staring an inner block.\n";
        StringBad headline1("Celery Stalks at Midnight");
        StringBad headline2("Lwttuce Prey");
        StringBad sports("Spanish Leaves Bowl for Dollars");

        cout << "headline1: " << headline1 << endl;
        cout << "headline2: " << headline2 << endl;
        cout << "sports: " << sports << endl;
        callme1(headline1);
        cout << "headline1: " << headline1 << endl;
        callme2(headline2);
        //headline2作为函数参数来传递从而导致析构函数被调用
        //按值传递意味着创建原始变量的一个副本
        //按值传递对象将调用复制构造函数,因该按引用传递对象
        //这样可以节省调用构造函数的时间及储存新对象的空间
        cout << "headline2: " << headline2 << endl;
        cout << "Initialize one object to another:\n";
        StringBad sailor = sports;
        //等效于StringBad sailor = StringBad(sports);
        //复制构造函数原型: Class_name(const Class_name &);
        //相应的构造函数为StringBad(const StringBad &);
        //使用一个对象来初始化另一个对象时,编译器将自动生成上述构造函数(称为复制构造函数,创建了对象的一个副本)
        
        cout << "sailor: " << sailor << endl;
        cout << "Assign one object to another:\n";
        StringBad knot;
        knot = headline1;
        cout << "knot: " << knot << endl;
        cout << "Exiting the block.\n";
    }
    cout << "End of main()\n";
    system("pause");
    return 0;
}

void calme1(StringBad &rsb)
{
    cout << "String passed by reference:\n";
    cout << "    \"" << rsb << "\"\n";
}
void calme2(StringBad sb)
{
    cout << "String passed by value:\n";
    cout << "    \"" << sb << "\"\n";
}
